package com.hunliji.monitor.share.utils;

import com.google.common.collect.Lists;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.util.StringUtils;

/**
 * 字符串公共方法
 * @author wuchen
 */
public class StringCommonUtil {
    /**
     * 数字转换成一个固定位数的数字字符串
     * @param num
     * @param formatLen
     * @return
     */
    public static String getFormatFromInteger(Integer num,Integer formatLen){
        String format = "%0"+formatLen+"d";
        return String.format(format,num);
    }

    /**
     * 用正则表达式提取字符串中@人的字符串
     */
    public static List<String> getAtUser(String text) {
        if(StringUtils.isEmpty(text)){
            return null;
        }
        Pattern pattern = Pattern.compile("@[\\u4e00-\\u9fa5\\w]+");
        Matcher matcher = pattern.matcher(text);
        List<String> result = Lists.newArrayList();
        while (matcher.find()) {
            result.add(matcher.group().replace("@","").replace(" ", ""));
        }
        return result;
    }
}
